﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Transformer.iHunter.UI
{
    public partial class HtmlTextBox : WebBrowser
    {
        private const string EDITOR_ID = "theTextbox";

        public HtmlTextBox()
        {
            InitializeComponent();

            Init();
        }
        
        private void Init()
        {
            this.DocumentText = Properties.Resources.HtmlContent;

            this.ObjectForScripting = this;
        }

        protected override void OnEnter(EventArgs e)
        {
            base.OnEnter(e);

            if (HtmlEditorEnter != null)
                HtmlEditorEnter(this, EventArgs.Empty);

            GetFocus();
        }

        protected override void OnLeave(EventArgs e)
        {
            base.OnLeave(e);

            if (HtmlEditorLeave != null)
                HtmlEditorLeave(this, EventArgs.Empty);
        }
        
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

        public void OnEventMouseUp()
        {
            if (this.HtmlEditorMouseUp != null)
                this.HtmlEditorMouseUp(this, EventArgs.Empty);
        }

        public void OnEventKeyDown()
        {
            if (this.HtmlEditorKeyDown != null)
                this.HtmlEditorKeyDown(this, EventArgs.Empty);
        }

        public event EventHandler HtmlEditorKeyDown;
        public event EventHandler HtmlEditorMouseUp;
        public event EventHandler HtmlEditorEnter;
        public event EventHandler HtmlEditorLeave;

        public string HtmlText
        {
            get
            {
                string html = string.Empty;

                try
                {
                    html = this.Document.All[EDITOR_ID].InnerHtml;
                }
                catch { }

                if (null == html)
                    html = string.Empty;

                return html;
            }
            set
            {
                string html = value;
                if (null == html)
                    html = string.Empty;
                try
                {
                    this.Document.All[EDITOR_ID].InnerHtml = html;
                }
                catch { }
            }
        }

        #region Editor Commands

        public void Cut()
        {

            this.Document.ExecCommand("Cut", false, null);

            this.OnWebBrowserCommandExecuted();

        }

        public void Paste()
        {

            this.Document.ExecCommand("Paste", false, null);

            this.OnWebBrowserCommandExecuted();

        }

        public void Copy()
        {

            this.Document.ExecCommand("Copy", false, null);

            this.OnWebBrowserCommandExecuted();

        }

        public void Undo()
        {

            this.Document.ExecCommand("Undo", false, null);

            this.OnWebBrowserCommandExecuted();

        }

        public void Redo()
        {
            this.Document.ExecCommand("Redo", false, null);

            this.OnWebBrowserCommandExecuted();
        }


        public void Bold()
        {
            this.Document.ExecCommand("Bold", false, null);

            this.OnWebBrowserCommandExecuted();
        }

        public void Italic()
        {
            this.Document.ExecCommand("Italic", false, null);

            this.OnWebBrowserCommandExecuted();
        }

        public void InsertOrderedList()
        {
            this.Document.ExecCommand("InsertOrderedList", false, null);

            this.OnWebBrowserCommandExecuted();
        }

        public void InsertUnOrderedList()
        {
            this.Document.ExecCommand("InsertUnOrderedList", false, null);

            this.OnWebBrowserCommandExecuted();
        }

        public void Outdent()
        {
            this.Document.ExecCommand("Outdent", false, null);

            this.OnWebBrowserCommandExecuted();
        }

        public void Indent()
        {
            this.Document.ExecCommand("Indent", false, null);

            this.OnWebBrowserCommandExecuted();
        }

        public void Underline()
        {
            this.Document.ExecCommand("Underline", false, null);

            this.OnWebBrowserCommandExecuted();
        }

        public void StrikeThrough()
        {
            this.Document.ExecCommand("StrikeThrough", false, null);

            this.OnWebBrowserCommandExecuted();
        }


        public void SetFont(string fontName)
        {
            this.Document.ExecCommand("FontName", false, fontName);

            this.OnWebBrowserCommandExecuted();
        }

        public void SetFontSize(int size)
        {
            this.Document.ExecCommand("FontSize", false, size);

            this.OnWebBrowserCommandExecuted();
        }

        public void SetForeColor(int red, int green, int blue)
        {
            this.Document.ExecCommand("ForeColor", false,
                string.Format("#{0}{1}{2}",
                red.ToString("X2"),
                green.ToString("X2"),
                blue.ToString("X2")
                ));

            this.OnWebBrowserCommandExecuted();
        }

        public void SetBackColor(int red, int green, int blue)
        {
            this.Document.ExecCommand("BackColor", false,
                string.Format("#{0}{1}{2}",
                red.ToString("X2"),
                green.ToString("X2"),
                blue.ToString("X2")
                ));

            this.OnWebBrowserCommandExecuted();
        }

        public void Superscript()
        {
            this.Document.ExecCommand("Superscript", false, null);

            this.OnWebBrowserCommandExecuted();
        }

        public void Subscript()
        {
            this.Document.ExecCommand("Subscript", false, null);

            this.OnWebBrowserCommandExecuted();
        }

        public void InsertHorizontalLine()
        {
            this.Document.ExecCommand("InsertHorizontalRule", false, null);

            this.OnWebBrowserCommandExecuted();
        }

        public void FormatBlock(string tag)
        {
            this.Document.ExecCommand("FormatBlock", false, tag);

            this.OnWebBrowserCommandExecuted();
        }

        public void JustifyLeft()
        {
            this.Document.ExecCommand("JustifyLeft", false, null);

            this.OnWebBrowserCommandExecuted();
        }

        public void JustifyRight()
        {
            this.Document.ExecCommand("JustifyRight", false, null);

            this.OnWebBrowserCommandExecuted();
        }

        public void JustifyFull()
        {
            this.Document.ExecCommand("JustifyFull", false, null);

            this.OnWebBrowserCommandExecuted();
        }

        public void JustifyCenter()
        {
            this.Document.ExecCommand("JustifyCenter", false, null);

            this.OnWebBrowserCommandExecuted();
        }

        public void JustifyNone()
        {
            this.Document.ExecCommand("JustifyNone", false, null);

            this.OnWebBrowserCommandExecuted();
        }

        public void ClearFormat()
        {
            this.Document.ExecCommand("RemoveFormat", false, null);

            this.OnWebBrowserCommandExecuted();
        }

        public void SelectAll()
        {
            this.Document.ExecCommand("SelectAll", false, null);

            //this.OnWebBrowserCommandExecuted();
        }

        public void Delete()
        {
            this.Document.ExecCommand("Delete", false, null);
        }

        #endregion

        public void GetFocus()
        {
            this.Document.InvokeScript("InitFocus");
        }

        private void OnWebBrowserCommandExecuted()
        {
            GetFocus();
        }
    }
}
